<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
    <title>任务管理</title>
    <meta name="decorator" content="default"/>
    <%@include file="/WEB-INF/views/include/treeview.jsp" %>
    <link href="${ctxStatic}/jqGrid/4.6/css/ui.jqgrid.css" type="text/css" rel="stylesheet" />
    <script src="${ctxStatic}/jqGrid/4.7/js/jquery.jqGrid.js" type="text/javascript"></script>
    <script src="${ctxStatic}/jqGrid/4.7/js/jquery.jqGrid.extend.js" type="text/javascript"></script>
</head>
<body>
<ul class="nav nav-tabs">
    <li class="active"><a href="${ctx}/devmng/taskMain/taskMainType?id=${taskMain.id}">任务<shiro:hasPermission name="devmng:taskMain:edit">${not empty taskMain.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="devmng:taskMain:edit">查看</shiro:lacksPermission></a></li>
</ul><br/>
<div>
    <form:form id="inputForm" modelAttribute="taskMain" action="${ctx}/devmng/taskMain/save" method="post" class="form-horizontal">
        <form:hidden path="id"/>
        <sys:message content="${message}"/>

        <%--<div class="control-group" style="float:left">
            <div id="content">
                <div id="left" class="accordion-group">
                    <div class="accordion-heading">
                        <a class="accordion-toggle">设备管理<i class="icon-refresh pull-right" onclick="refreshTree();"></i></a>
                    </div>
                    <input type="hidden" id="deviceIds" name="deviceIds" value="" />
                    <input type="hidden" id="deviceNames" name="deviceNames" value="" />
                    <input type="hidden" id="flag" name="flag" value="Y" />
                    <div id="ztree" class="ztree"></div>
                </div>
                <div id="openClose" class="close">&nbsp;</div>
            </div>
        </div>--%>
        <input type="hidden" id="flag" name="flag" value="Y" />
        <div class="control-group">

            <label class="control-label">任务标题：</label>
            <div class="controls">
                <form:input path="taskTitle" htmlEscape="false" value="任务:" maxlength="255" class="input-xlarge required"/>
                <span class="help-inline"><font color="red">*计划任务标题自动生成</font> </span>
            </div>
            <br>

            <label class="control-label">开始时间：</label>
            <div class="controls">
                <input name="taskStart" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate required"
                       value="<fmt:formatDate value="${taskMain.taskStart}" pattern="yyyy-MM-dd HH:mm:ss"/>"
                       onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
                <span class="help-inline"><font color="red">*</font> </span>
            </div>
            <br>

            <label class="control-label">结束时间：</label>
            <div class="controls">
                <input name="taskEnd" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate required"
                       value="<fmt:formatDate value="${taskMain.taskEnd}" pattern="yyyy-MM-dd HH:mm:ss"/>"
                       onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
                <span class="help-inline"><font color="red">*</font> </span>
            </div>
            <br>

            <label class="control-label">操作人：</label>
            <div class="controls">
                <sys:treeselect id="taskOperator" name="taskOperator.id" value="${taskMain.taskOperator.id}" labelName="taskOperator.name" labelValue="${taskMain.taskOperator.name}"
                                title="用户" url="/sys/office/treeData?type=3" cssClass="required" allowClear="true" notAllowSelectParent="true"  />
               <span class="help-inline"><font color="red">*</font> </span>
            </div>


            <br>

            <label class="control-label">任务类型：</label>
            <div class="controls">
                <form:select path="taskType" class="input-xlarge required">
                    <form:option value="" label=""/>
                    <form:options items="${fns:getDictList('DEVCHK_PERIOD')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                </form:select>
                <span class="help-inline"><font color="red">*</font> </span>
            </div>
            <br>

            <label class="control-label">部门：</label>
            <div class="controls">
                <sys:treeselect id="sysOffice" name="sysOffice.id" value="${taskMain.sysOffice.id}" labelName="sysOffice.name" labelValue="${taskMain.sysOffice.name}"
                                title="部门" url="/sys/office/treeData?type=2" cssClass="required" allowClear="true" notAllowSelectParent="true" disabled="false"/>
                <span class="help-inline"><font color="red">*</font> </span>
            </div>
            <br>

            <label class="control-label">备注：</label>
            <div class="controls">
                <form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
            </div>
            <br>

            <div class="controls">
                <label class="control-label"></label>
            <shiro:hasPermission name="devmng:taskMain:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
                <input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
            </div>
        </div>
    </form:form>


    <%--<button onclick="monthFlagCheck();">monthFlagCheck</button>--%>
    <%--<button onclick="showData();">查看数据</button>--%>
    <%--<button onclick="startEdit();">开始</button>--%>
    <%--<button onclick="saveRows();">保存</button>--%>


</div>
<script type="text/javascript">

    function startEdit() {
        var grid = $("#dataGrid");
        var ids = grid.jqGrid('getDataIDs');

        for (var i = 0; i < ids.length; i++) {
            grid.jqGrid('editRow',ids[i]);
        }
    };

    function saveRows() {
        var grid = $("#dataGrid");
        var ids = grid.jqGrid('getDataIDs');

        for (var i = 0; i < ids.length; i++) {
            grid.jqGrid('saveRow', ids[i]);
        }
    }

    function showData() {
        var grid = jQuery('#dataGrid');
        var rowKey = grid.jqGrid('getGridParam',"selrow");
        var row = grid.jqGrid("getRowData", rowKey);
        console.log(row);
    }

    var leftWidth = 280; // 左侧窗口大小
    var htmlObj = $("html");
    var frameObj = $("#left, #openClose, #right, #rightContent");

    function wSize(){
        // var strs = getWindowSize().toString().split(",");
        // htmlObj.css({"overflow-x":"hidden", "overflow-y":"hidden"});
        frameObj.height(500);
        var leftWidth = ($("#left").width() < 0 ? 0 : $("#left").width());
        $("#right").width($("#content").width()- leftWidth - $("#openClose").width() -30);
        $(".ztree").width(leftWidth - 10).height(frameObj.height() - 46).css("overflow","auto");
    }

    function zTreeOnCheck(event, treeId, treeNode) {
        var deviceIds = "";
        var deviceNames = "";
        console.log(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
        var id = treeNode.id == '0' ? '' :treeNode.id;
        var grid = jQuery('#dataGrid');
        grid.jqGrid("clearGridData");
        var treeObj = $.fn.zTree.getZTreeObj("ztree");
        var nodes = treeObj.getCheckedNodes(true);
//        for(var node in nodes){
        for (var i = 0; i < nodes.length; i++) {
            var node = nodes[i];
            if (node.id.indexOf("dept_") >= 0) {
                continue;
            }

            grid.jqGrid("addRowData", node.id, {deviceMainId: node.id, deviceName: node.name, monthFlag: "N", quarterFlag: "N", halfYearFlag: "N", yearFlag: "N" })
            deviceIds += node.id + ",";
            deviceNames += node.name + ",";

        }
        document.getElementById("deviceIds").value = deviceIds;
        document.getElementById("deviceNames").value = deviceNames;
        console.log( document.getElementById("deviceIds").value = deviceIds);
        console.log( document.getElementById("deviceNames").value = deviceNames);
//        if (treeNode.checked) {
//            grid.jqGrid("addRowData", treeNode.id, {deviceMainId: treeNode.id, deviceName: treeNode.name, monthFlag: "N", quarterFlag: "N", halfYearFlag: "N", yearFlag: "N" })
//        } else {
//            grid.jqGrid("delRowData", treeNode.id);
//        }
    };

    var setting = {
        data: {
            simpleData:{
                enable:true,
                idKey:"id",
                pIdKey:"pId",
                rootPId:'0'
            }
        },
        check: {
            enable: true
        },
        callback:{
            onCheck: zTreeOnCheck
        }
    };

    function refreshTree(){


        $.getJSON("${ctx}/devmng/deviceMain/deptTreeDataForEdit?taskMainId=${taskMain.id}",function(data){
            var treeObj = $.fn.zTree.init($("#ztree"), setting, data);
            var nodes = treeObj.getNodeByTId("dept_1");
            treeObj.expandNode(nodes, true, false, false);

        });
    }

    var lastSelection;
    $(document).ready(function() {
        //$("#name").focus();
        $("#inputForm").validate({
            submitHandler: function(form){
//                var ids = [], nodes = tree.getCheckedNodes(true);
//                for(var i=0; i<nodes.length; i++) {
//                    ids.push(nodes[i].id);
//                }
//                $("#deviceMainIds").val(ids);

                loading('正在提交，请稍等...');
                form.submit();
            },
            errorContainer: "#messageBox",
            errorPlacement: function(error, element) {
                $("#messageBox").text("输入有误，请先更正。");
                if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                    error.appendTo(element.parent().parent());
                } else {
                    error.insertAfter(element);
                }
            }


        });
        refreshTree();
    });
</script>
<script src="${ctxStatic}/common/wsize.min.js" type="text/javascript"></script>
</body>
</html>
